package com.badlgic.androidgames;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.SpriteBackground;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.os.Handler;


public class Line98Activity extends BaseGameActivity implements IOnMenuItemClickListener {
	private static final int CAMERA_WIDTH = 768;
	private static final int CAMERA_HEIGHT = 1024;
	private Scene scene;
	protected MenuScene mMenuScene;
	
	protected Handler mHandler;

	protected static final int MENU_PLAY = 0;
	protected static final int MENU_ACHIEVEMENT = MENU_PLAY + 1;
	protected static final int MENU_OPTION = MENU_ACHIEVEMENT + 1;

	protected Camera mCamera;

	private BitmapTextureAtlas mBackgroundBitmapTextureAtlas;
	private TextureRegion mBackgroundTextureRegion;

	private BitmapTextureAtlas mPlayBitmapTextureAtlas;
	private BitmapTextureAtlas mAchievementBitmapTextureAtlas;
	private BitmapTextureAtlas mOptionBitmapTextureAtlas;

	protected TextureRegion mPlayTextureRegion;
	protected TextureRegion mAchievementTextureRegion;
	protected TextureRegion mOptionTextureRegion;

	@Override
	public Engine onLoadEngine() {
		mHandler = new  Handler();
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(
				CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
	}

	@Override
	public void onLoadResources() {
		this.mBackgroundBitmapTextureAtlas = new BitmapTextureAtlas(1024, 1024,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.mBackgroundBitmapTextureAtlas, this, "gfx/line_man.jpg", 0, 0);

		this.mPlayBitmapTextureAtlas = new BitmapTextureAtlas(512, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mPlayTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.mPlayBitmapTextureAtlas, this, "gfx/bt_play.png", 0, 0);

		this.mAchievementBitmapTextureAtlas = new BitmapTextureAtlas(512, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mAchievementTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.mAchievementBitmapTextureAtlas, this, "gfx/bt_achievement.png", 0, 0);

		this.mOptionBitmapTextureAtlas = new BitmapTextureAtlas(256, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mOptionTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.mOptionBitmapTextureAtlas, this, "gfx/bt_option.png", 0, 0);

		this.mEngine.getTextureManager().loadTexture(this.mBackgroundBitmapTextureAtlas);
		this.mEngine.getTextureManager().loadTexture(this.mPlayBitmapTextureAtlas);
		this.mEngine.getTextureManager().loadTexture(this.mOptionBitmapTextureAtlas);
		this.mEngine.getTextureManager().loadTexture(this.mAchievementBitmapTextureAtlas);
	}

	@Override
	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());
		this.createMenuScene();
		
		scene = new Scene();
		Sprite sprite = new Sprite(0, 0, mBackgroundTextureRegion);
		Sprite playSprite = new Sprite(432, 412, mPlayTextureRegion) {
			protected void onManagedUpdate(float pSecondsElapsed) {
				super.onManagedUpdate(pSecondsElapsed);
			}
			
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				
				if(pSceneTouchEvent.isActionDown())
				{
				}
				return true;
			}
		};
		Sprite achievementSprite = new Sprite(366, 508, mAchievementTextureRegion) {
			protected void onManagedUpdate(float pSecondsElapsed) {
   				
   				super.onManagedUpdate(pSecondsElapsed);
   			}

   			@Override
   			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
   					float pTouchAreaLocalX, float pTouchAreaLocalY) {
   				
   				if(pSceneTouchEvent.isActionDown())
   				{
   					Intent start = new Intent(Line98Activity.this, HighScores.class);
   					startActivity(start);	
   				}
   				return true;
   			}
		};
		Sprite optionSprite = new Sprite(420, 593, mOptionTextureRegion) {
			protected void onManagedUpdate(float pSecondsElapsed) {
   				
   				super.onManagedUpdate(pSecondsElapsed);
   			}

   			@Override
   			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
   					float pTouchAreaLocalX, float pTouchAreaLocalY) {
   				
   				if(pSceneTouchEvent.isActionDown())
   				{
   					Line98Activity.this.finish();
   					return true;
   				}
   				return true;
   			}
		};
		scene.setBackground(new SpriteBackground(sprite));
		
		scene.attachChild(playSprite);
		scene.attachChild(achievementSprite);
		scene.attachChild(optionSprite);
		
		scene.setTouchAreaBindingEnabled(true);
		scene.registerTouchArea(playSprite);
		scene.registerTouchArea(achievementSprite);
		scene.registerTouchArea(optionSprite);
		return scene;
	}

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub

	}

	public void createMenuScene() {
		this.mMenuScene = new MenuScene(this.mCamera);

		final SpriteMenuItem playMenuItem = new SpriteMenuItem(MENU_PLAY, mPlayTextureRegion);
		playMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.mMenuScene.addMenuItem(playMenuItem);

		final SpriteMenuItem achievementMenuItem = new SpriteMenuItem(MENU_ACHIEVEMENT,
				mAchievementTextureRegion);
		achievementMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.mMenuScene.addMenuItem(achievementMenuItem);

		final SpriteMenuItem optionMenuItem = new SpriteMenuItem(MENU_OPTION, mOptionTextureRegion);
		optionMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.mMenuScene.addMenuItem(optionMenuItem);

		this.mMenuScene.buildAnimations();
		this.mMenuScene.setBackgroundEnabled(false);
		this.mMenuScene.setOnMenuItemClickListener(this);
	}
	
	private Runnable highScoresMenu = new Runnable() {
		
		@Override
		public void run() {
			Intent myIntent = new Intent(Line98Activity.this, HighScores.class);
			Line98Activity.this.startActivity(myIntent);
		}
	};
	

	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX,
			float pMenuItemLocalY) {
		// TODO Auto-generated method stub
		return false;
	}
		
}